<?php
namespace Utuo;

use Utuo\DistributionShopScore\AbstractScore;

class DistributionShopScore {
    
    public static $config = array(
        'score_table' => 'distribution_shop_user_score',
        'score_record_table' => 'distribution_shop_score_record',
    );
    	
	/**
	 * 驱动实例
	 * @var Object
	 */
	private $payer;
	
    /**
     * 获得实例
     * @param string $driver
     * @param array $config 基础参数配置 
     *  array(
     *    'uid' => 0, // *
     *    'cid' => 0, // *
     *  }
     * @param array $driverConfig 驱动参数配置
     * @return
     */
    public final static function getInstance($driver, $config = array(), $driverConfig = array()) {
    	static $instance;
    	$config['tag'] = $driver;
    	if(!$instance) $instance = new DistributionShopScore($driver, $config, $driverConfig);
    	return $instance;
    }
    
    /**
     * 构造
     * @param unknown $driver Login
     * @param unknown $config 
     */
    public function __construct( $driver, $config = array(), $driverConfig = array() ) {
    	$class = "Utuo\\DistributionShopScore\\{$driver}Score";
    	class_exists($class) or $class = "Utuo\\DistributionShopScore\\CommonScore";
    	$driverConfig and $config = array_merge($config, array('tag' => $driver, 'driverConfig' => $driverConfig)); // 合并基本配置与驱动配置
    	$this->setDriver($class, $config); // 设置驱动
    }
    
    /**
     * 设置驱动
     * @param string $class 驱动类名称
     */
    private function setDriver( $class, $config ) {
    	$this->payer = new $class( $config );
    	if ( !$this->payer ) {
    		E("不存在驱动：{$class}");
    	}
    }
    
    public function __call($method, $arguments) {
    	if ( method_exists($this, $method) ) {
    		return call_user_func_array(array(&$this, $method), $arguments);
    	} elseif ( !empty($this->payer) && $this->payer instanceof AbstractScore && method_exists($this->payer, $method) ) {
    		return call_user_func_array(array(&$this->payer, $method), $arguments);
    	}
    }
    
}


